#include <stdio.h>
#include <stdlib.h>
#include <pthread.h>

#define SIZE 1000000000
#define N 10
#define CNT (SIZE/N)

struct param {
    int start;
    int end;
};

struct res {
    double sum;
};

void *compute(void *arg)
{
    struct param *param;
    struct res *res;
    param = (struct param *)arg;

    double sum = 0;
    int i;
    for (i = param->start; i < param->end; i ++) {
        if(i % 2) sum += (1.0 / (2 * i - 1));
        else sum -= (1.0 / (2 * i - 1));
    }

    res = malloc(sizeof(struct res));
    res->sum = sum;
    return res;
}

void main() { 
    pthread_t workers[N];
    struct param params[N];

    int i;
    for (i = 0; i < N; i ++) {
        struct param *param;
        param = &params[i];
        param->start = i * CNT;
        if(!i) param->start = 1;
        param->end = (i + 1) * CNT;
        pthread_create(&workers[i], NULL, compute, param);
    }

    double sum = 0;
    for (i = 0; i < N; i ++) {
        struct res *res;
        pthread_join(workers[i], (void **)&res);
        sum += res->sum;
        free(res);
    }

    printf("PI = %.8lf\n", 4 * sum);
}